//
//  UIViewController+Navigation.m
//  YJJ0
//
//  Created by dadaxueche on 16/6/21.
//  Copyright © 2016年 王—wagn. All rights reserved.
//

#import "UIViewController+Navigation.h"


#define kSpacerWidth 10.f
#define kBarButtonImageInset 10.f

static const CGFloat kBarButtonSize = 36.f;

@implementation UIViewController (Navigation)

-(void)setRightButtonImage:(UIImage *)image target:(id)target action:(SEL)action
{
    if (self.navigationItem) {
        
        if ([EmptyUtils isEmptyNSArray:self.navigationItem.rightBarButtonItems]) {
            UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
            rightButton.frame = CGRectMake(0, 0, kBarButtonSize, kBarButtonSize);
            [rightButton setImage:image forState:UIControlStateNormal];
            rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -kBarButtonSize);
            [rightButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
            
            UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
            UIBarButtonItem *negativeSpacer  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSpacer.width = -kSpacerWidth;
            self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, rightButtonItem, nil];
            
        } else {
    
            UIBarButtonItem *rightButtonItem = self.navigationItem.rightBarButtonItems[1];
            UIButton        *rightButton     = (UIButton *)rightButtonItem.customView;
            [rightButton setImage:image forState:UIControlStateNormal];
            [rightButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
            [rightButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)setRightSecondButtonImage:(UIImage *)image action:(SEL)action {
    if (self.navigationItem) {
        if (self.navigationItem.rightBarButtonItems.count <= 2) {
            UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
            rightButton.frame = CGRectMake(0, 0, kBarButtonSize, kBarButtonSize);
            [rightButton setImage:image forState:UIControlStateNormal];
            rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -kBarButtonImageInset);
            [rightButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *secondRightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
            
            NSMutableArray *rightButtonItems = [NSMutableArray array];
            if (self.navigationItem.rightBarButtonItems) {
                rightButtonItems = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems];
            }
            [rightButtonItems addObject:secondRightItem];
            
            self.navigationItem.rightBarButtonItems = rightButtonItems;
        } else {
            UIBarButtonItem *rightButtonItem = self.navigationItem.rightBarButtonItems[2];
            UIButton        *rightButton     = (UIButton *)rightButtonItem.customView;
            [rightButton setImage:image forState:UIControlStateNormal];
            [rightButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
            [rightButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)hideTopRightButton {
    if (self.navigationItem) {
        self.navigationItem.rightBarButtonItems = @[];
    }
}

- (void)setLeftButtonImage:(UIImage *)image action:(SEL)action {
    if (self.navigationItem) {
        if ([EmptyUtils isEmptyNSArray:self.navigationItem.leftBarButtonItems]) {
            UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
            leftButton.frame = CGRectMake(0, 0, kBarButtonSize, kBarButtonSize);
            [leftButton setImage:image forState:UIControlStateNormal];
            leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -kBarButtonImageInset, 0, 0);
            [leftButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
            
            UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
            UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSpacer.width                   = -kSpacerWidth;
            self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, leftButtonItem, nil];
        } else {
            UIBarButtonItem *leftButtonItem = self.navigationItem.leftBarButtonItems[1];
            UIButton        *leftButton     = (UIButton *)leftButtonItem.customView;
            [leftButton setImage:image forState:UIControlStateNormal];
            [leftButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
            [leftButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)replaceLeftButtonImage:(UIImage *)image {
    if (self.navigationItem) {
        if (![EmptyUtils isEmptyNSArray:self.navigationItem.leftBarButtonItems]) {
            UIBarButtonItem *leftButtonItem = self.navigationItem.leftBarButtonItems[1];
            UIButton        *leftButton     = (UIButton *)leftButtonItem.customView;
            [leftButton setImage:image forState:UIControlStateNormal];
        }
    }
}

- (void)replaceRightButtonImage:(UIImage *)image {
    if (self.navigationItem) {
        if (![EmptyUtils isEmptyNSArray:self.navigationItem.rightBarButtonItems]) {
            UIBarButtonItem *rightButtonItem = self.navigationItem.rightBarButtonItems[1];
            UIButton        *rightButton     = (UIButton *)rightButtonItem.customView;
            [rightButton setImage:image forState:UIControlStateNormal];
        }
    }
}

- (void)setRightButtonTitle:(NSString *)title font:(UIFont *)font action:(SEL)action {
    self.navigationItem.rightBarButtonItems = nil;
    
    if (self.navigationItem) {
        if (!self.navigationItem.rightBarButtonItem) {
            UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
            
            //            if (font != nil) {
            //                NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
            //                [rightButtonItem setTitleTextAttributes:dict forState:UIControlStateNormal];
            //            }
            
            [self.navigationItem setRightBarButtonItem:rightButtonItem];
        } else {
            [self.navigationItem.rightBarButtonItem setTitle:title];
        }
    }
}

- (void)setLeftButtonTitle:(NSString *)title font:(UIFont *)font action:(SEL)action {
    self.navigationItem.leftBarButtonItems = nil;
    
    if (self.navigationItem) {
        if (self.navigationItem.leftBarButtonItem == nil) {
            UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
            
            //            if (font != nil) {
            //                NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
            //                [leftButtonItem setTitleTextAttributes:dict forState:UIControlStateNormal];
            //            }
            
            [self.navigationItem setLeftBarButtonItem:leftButtonItem];
        } else {
            [self.navigationItem.leftBarButtonItem setTitle:title];
        }
    }
}

- (void)setBackButton:(NSString *)backTitle font:(UIFont *)font {
    if (self.navigationItem) {
        UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] init];
        if (backTitle != nil) {
            backButtonItem.title = backTitle;
        }
        
        if (font != nil) {
            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
            [backButtonItem setTitleTextAttributes:dict forState:UIControlStateNormal];
        }
        
        [self.navigationItem setBackBarButtonItem:backButtonItem];
    }
}

- (void)setYJJLeftButton {
    if (self.navigationItem) {
        
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.frame = CGRectMake(0, 0, kBarButtonSize, kBarButtonSize);
        [leftButton setImage:[UIImage imageNamed:@"back_organge"] forState:UIControlStateNormal];
        [leftButton setImage:[UIImage imageNamed:@"back_gray"] forState:UIControlStateDisabled];
        leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -kBarButtonImageInset, 0, 0);
        [leftButton addTarget:self action:@selector(returnToPreviousPage) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSpacer.width = -kSpacerWidth;
        
        self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, leftButtonItem, nil];
    }
}

- (void)disableYJJLeftButton {
    [self changeLeftBtnState:NO];
}

- (void)enableYJJLeftButton {
    [self changeLeftBtnState:YES];
}

- (void)disableTitleRightButton {
    [self changeRightBtnState:NO];
}

- (void)enableTitleRightButton {
    [self changeRightBtnState:YES];
}

- (void)changeLeftBtnState:(BOOL)enableOrNot {
    if (self.navigationItem && self.navigationItem.leftBarButtonItems) {
        UIBarButtonItem *leftButtonItem = self.navigationItem.leftBarButtonItems[1];
        [leftButtonItem setEnabled:enableOrNot];
    } else if (self.navigationItem && self.navigationItem.leftBarButtonItem) {
        [self.navigationItem.leftBarButtonItem setEnabled:enableOrNot];
    }
}

- (void)changeRightBtnState:(BOOL)enableOrNot {
    if (self.navigationItem && self.navigationItem.rightBarButtonItems) {
        UIBarButtonItem *rightButtonItem = self.navigationItem.rightBarButtonItems[0];
        [rightButtonItem setEnabled:enableOrNot];
    } else if (self.navigationItem && self.navigationItem.rightBarButtonItem) {
        [self.navigationItem.rightBarButtonItem setEnabled:enableOrNot];
    }
}

- (void)returnToPreviousPage {
    if (self.navigationController != nil) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)setNaviTitleView:(UIView *)view {
    if (self.navigationItem) {
        [self.navigationItem setTitleView:view];
    }
}


@end
